
import os
import requests
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from lxml import etree
from selenium.webdriver.chrome.options import Options
from bs4 import BeautifulSoup

from readability import Document
import docx
def image_download(url):
    s = Service('../chromedriver.exe')
    # 配置浏览器选项
    options = Options()
    options.add_argument("--headless")  # 无头模式
    driver = webdriver.Chrome(service=s, options=options)
    driver.get(url)
    page = driver.page_source
    driver.quit()
    path = etree.HTML(page)
    img = path.xpath('//img/@src')
    img1 = path.xpath('//img/@srcset')
    img2= []
    for i in img:
        if i[0:4] == 'http':
            img2.append(i)
        else:
            img2.append('http:'+i)
    return img2
def image_download2(img2):
    imgname = 0
    if os.path.exists('../临时'):
        pass
    else:
        os.mkdir('../临时')
    for j in img2:
        imgurl=requests.get(j)
        with open(f'../临时/picture{imgname}.jpg', 'wb') as f:
            f.write(imgurl.content)
        if imgname==1:
            imgname=imgname*10
        else:
            imgname = imgname + 1
        print(j)


def text_download(url):
    s = Service('../chromedriver.exe')
    # 配置浏览器选项
    options = Options()
    options.add_argument("--headless")  # 无头模式
    driver = webdriver.Chrome(service=s, options=options)
    driver.get(url)
    page = driver.page_source
    driver.quit()
    doc = Document(page)
    title = doc.title()
    content = doc.summary()  # 提取正文
    soup = BeautifulSoup(content, 'lxml')
    # 提取所有段落（根据实际标签调整，如 'div', 'br' 等）
    paragraphs = []
    for element in soup.find_all(['p', 'br']):
        if element.name == 'br':
            paragraphs.append('\n')  # 换行符
        else:
            text = element.get_text(strip=True)
            if text:
                paragraphs.append(text)
    # 合并段落并保留换行
    formatted_text = '\n'.join(paragraphs)
    print(title)
    print(formatted_text)
    return title, formatted_text


def save_to_word(title_text, body_text, file_path):
    """
    将标题和正文保存为 Word 文件
    :param title_text: 标题文本
    :param body_text: 正文文本
    :param file_path: 保存文件的路径
    """
    try:
        # 创建一个新的 Word 文档
        doc = docx.Document()

        # 添加标题
        doc.add_heading(title_text, level=1)

        # 添加正文
        doc.add_paragraph(body_text)

        # 保存文档
        doc.save(file_path)
        print(f"文件已成功保存到 {file_path}")
    except Exception as e:
        print(f"保存文件时出错: {e}")
